package com.youlai.boot.system.controller;

import com.youlai.boot.common.excel.ExcelOperationService;
import com.youlai.boot.common.result.ExcelResult;
import com.youlai.boot.system.service.ProductsingleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.ProductsingleForm;
import com.youlai.boot.system.model.query.ProductsingleQuery;
import com.youlai.boot.system.model.vo.ProductsingleVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;

/**
 * 单品跟进前端控制层
 *
 * @author xiheforever
 * @since 2025-05-16 23:05
 */
@Tag(name = "单品跟进接口")
@RestController
@RequestMapping("/api/v1/productsingle")
@RequiredArgsConstructor
public class ProductsingleController  {

    private final ProductsingleService productsingleService;
    private final ExcelOperationService excelOperationService;

    @Operation(summary = "单品跟进分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:productsingle:query')")
    public PageResult<ProductsingleVO> getProductsinglePage(ProductsingleQuery queryParams ) {
        IPage<ProductsingleVO> result = productsingleService.getProductsinglePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增单品跟进")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:productsingle:add')")
    public Result<Void> saveProductsingle(@RequestBody @Valid ProductsingleForm formData ) {
        boolean result = productsingleService.saveProductsingle(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取单品跟进表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:productsingle:edit')")
    public Result<ProductsingleForm> getProductsingleForm(
        @Parameter(description = "单品跟进ID") @PathVariable Long id
    ) {
        ProductsingleForm formData = productsingleService.getProductsingleFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改单品跟进")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:productsingle:edit')")
    public Result<Void> updateProductsingle(
            @Parameter(description = "单品跟进ID") @PathVariable Long id,
            @RequestBody @Validated ProductsingleForm formData
    ) {
        boolean result = productsingleService.updateProductsingle(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除单品跟进")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:productsingle:delete')")
    public Result<Void> deleteProductsingles(
        @Parameter(description = "单品跟进ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = productsingleService.deleteProductsingles(ids);
        return Result.judge(result);
    }

    @Operation(summary = "导出单品跟进Excel")
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPerm('system:productsingle:export')")
    public void exportExcel(ProductsingleQuery queryParams, HttpServletResponse response) {
        excelOperationService.exportExcel(queryParams, "productsingle", response);
    }

    @Operation(summary = "导入单品跟进Excel")
    @PostMapping("/import")
    @PreAuthorize("@ss.hasPerm('system:productsingle:import')")
    public Result<ExcelResult> importExcel(@RequestParam("file") MultipartFile file) {
        ExcelResult result = excelOperationService.importExcel(file, "productsingle");
        return Result.success(result);
    }

    @Operation(summary = "下载单品跟进导入模板")
    @GetMapping("/template")
    @PreAuthorize("@ss.hasPerm('system:productsingle:template')")
    public void downloadTemplate(HttpServletResponse response) {
        excelOperationService.downloadTemplate("productsingle", response);
    }
}
